package com.zhangpeng520.reAttend.Service;


import com.zhangpeng520.reAttend.dao.ReAttendMapper;
import com.zhangpeng520.reAttend.entity.ReAttend;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/20 0020.
 */

@Service
public class ReAttendServpml implements ReAttendService {

    static final String REATTEND_FLOW_ID="re_attend";


    final byte REATTEND_STATUS_ONGOING=1;//处理中
    final byte REATTEND_STATUS_PASS=2;//审批通过
    final byte REATTEND_STATUS_NOTPASS=3;//审批未通过

    /*任务关联补签数据键*/
    final String RE_ATTEND_SIGN="re_attend";

    /*下一个补签处理者*/
    final String NEXT_HANDLER="next_handler";

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;


    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ReAttendMapper reAttendMapper;

    /**
     *
     *@Author zhangpeng(www.zhangpeng520.com)
     *@Date 2017/8/20 0020 13:39
     *@Desripition 启动审批，用户补签
     *
     */

    @Override
    public void startReAttendflow(ReAttend reAttend) {

       /*从企业架构获取领导信息，把处理者设为该领导*/
        reAttend.setReAttendHandler("老李");
        reAttend.setStatus(REATTEND_STATUS_ONGOING);

         /*插入数据库buq表*/
        reAttendMapper.insertSelective(reAttend);

        /*启动补签流程实例*/
        Map<String,Object> map=new HashMap<>();

        map.put(RE_ATTEND_SIGN,reAttend);
        map.put(NEXT_HANDLER,reAttend.getReAttendHandler());

         /*启动补签流程实例*/
        ProcessInstance processInstance=runtimeService.startProcessInstanceByKey(REATTEND_FLOW_ID,map);
        Task task=taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        taskService.complete(task.getId(),map);

    }

    @Override
    public List<ReAttend> listTasks(String username) {

        List<ReAttend> reAttendList=new LinkedList<>();
        List<Task> taskList=taskService.createTaskQuery().processVariableValueEquals(username).list();
        if(CollectionUtils.isNotEmpty(taskList))
        {
            for (Task task:taskList) {
                Map<String,Object> variable=taskService.getVariables(task.getId());

                ReAttend reAttend=(ReAttend)variable.get(RE_ATTEND_SIGN);
                reAttend.setTaskId(task.getId());
                reAttendList.add(reAttend);
            }
        }

        return reAttendList;
    }

    @Override
    public void approve(ReAttend reAttend) {

        Task task=taskService.createTaskQuery().taskId(reAttend.getTaskId()).singleResult();
        if((""+REATTEND_STATUS_PASS).equals(reAttend.getApprovefalg()))
        {
            reAttend.setStatus(REATTEND_STATUS_PASS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }
        else{

            reAttend.setStatus(REATTEND_STATUS_NOTPASS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
        }
        taskService.complete(reAttend.getTaskId());

    }
}
